package com.shop.loan.handler;

import com.alibaba.fastjson.JSON;
import com.shop.loan.mapper.UserByMybatisMapper;
import com.shop.loan.model.entity.UserByMybatis;
import com.shop.loan.model.view.UserByOrderEnd;
import com.shop.loan.model.vo.UserRemarkVo;
import org.springframework.stereotype.Service;
import xyz.erupt.annotation.fun.OperationHandler;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserRemarkHandler implements OperationHandler<UserByOrderEnd, UserRemarkVo> {

    @Resource
    UserByMybatisMapper userByMybatisMapper;

    @Override
    public String exec(List<UserByOrderEnd> data, UserRemarkVo userRemarkVo, String[] param) {
        UserByOrderEnd userByOrderEnd = data.get(0);
        Long userId = userByOrderEnd.getId();

        UserByMybatis userByMybatis = new UserByMybatis();
        userByMybatis.setId(userId);
        userByMybatis.setMachineStatus(userRemarkVo.getUserStatus() != 1);
        userByMybatis.setUserWill(userRemarkVo.getUserWill());
        userByMybatis.setRemark(userRemarkVo.getRemark());
        System.out.println(JSON.toJSONString(userByMybatis));
        userByMybatisMapper.updateByPrimaryKeySelective(userByMybatis);
        return null;
    }

    @Override
    public UserRemarkVo eruptFormValue(List<UserByOrderEnd> data, UserRemarkVo userRemarkVo, String[] param) {
        UserByOrderEnd userByOrderEnd = data.get(0);
        Long userId = userByOrderEnd.getId();

        UserByMybatis userByMybatis = userByMybatisMapper.selectByPrimaryKey(userId);
        if(userByMybatis != null){
            userRemarkVo.setRemark(userByMybatis.getRemark());
            userRemarkVo.setUserWill(userByMybatis.getUserWill());
            if(userByMybatis.getMachineStatus() != null && userByMybatis.getMachineStatus()){
                userRemarkVo.setUserStatus(2);
            }else{
                userRemarkVo.setUserStatus(1);
            }
        }
        return userRemarkVo;
    }
}
